package com.pay.paytoken.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.pay.paytoken.R;
import com.pay.paytoken.base.BaseActivity;

import appUtils.LogUtil;
import appUtils.PhoneUtil;
import appUtils.SharedPreferencesManager;

import static appUtils.PhoneUtil.REQUEST_PERMISSION_READ_PHONE_STATE_CODE;

public class WelcomeActivity extends BaseActivity {
    private Handler mHandler;
    public static final String VERIFICATION = "verification";

    protected void onStart() {
        super.onStart();
        checkPermissions();
    }

    @Override
    public int setView() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initUtils();
        mHandler = new Handler();
    }

    private void initUtils() {
    }

    private void gotoHome() {
        String topActivityName = mSharedPreferencesManager.readString(SharedPreferencesManager.TOP_ACTIVITY, "");
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
        if (!TextUtils.isEmpty(topActivityName) && topActivityName.equals("PasswordVerificationActivity")) {
            intent.putExtra(VERIFICATION, 1);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public void initView() {
    }

    private boolean selfPermissionGranted(String permission) {
        // For Android < Android M, self permissions are always granted.
        boolean result = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            result = this.checkSelfPermission(permission)
                    == PackageManager.PERMISSION_GRANTED;
        }
        return result;
    }

    private void checkPermissions() {
        if (!selfPermissionGranted(Manifest.permission.READ_PHONE_STATE)) {
            requestReadContactsPermission();
        } else {
            Runnable mRunnable = new Runnable() {
                @Override
                public void run() {
                    gotoHome();
                }
            };
            mHandler.postDelayed(mRunnable, 2000);
        }
    }

    private void requestReadContactsPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                    PhoneUtil.REQUEST_PERMISSION_READ_PHONE_STATE_CODE);
        } else {
            mPhoneUtil.goSetting();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode != REQUEST_PERMISSION_READ_PHONE_STATE_CODE) {
            return;
        }
        if (grantResults.length > 0) {
            int grantResult = grantResults[0];
            boolean granted = grantResult == PackageManager.PERMISSION_GRANTED;
            boolean showRequestPermission = ActivityCompat.
                    shouldShowRequestPermissionRationale(WelcomeActivity.this, permissions[0]);
            LogUtil.log("onRequestPermissionsResult granted=" + granted);
            if (granted) {
                gotoHome();
                return;
            }
            if (showRequestPermission) {//
                checkPermissions();//checkPermissions again
            } else {
                mPhoneUtil.showPermissionDialog();//已经禁止(动态获取权限 不再询问 被勾选)
            }
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return keyCode != KeyEvent.KEYCODE_BACK && super.onKeyDown(keyCode, event);
    }
}
